package com.group.controller;

import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.dao.InviteRebateDao;
import com.group.model.*;
import com.group.model.In.RebateDeatailIn;
import com.group.model.Out.RebateDetailOut;
import com.group.service.InviteRebateService;
import com.group.util.NoteException;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * 邀请返佣接口
 * Cheney
 */
@RestController
@RequestMapping(value = "/InviteRebate")
public class InviteRebateController extends BaseController{

    org.slf4j.Logger logger = LoggerFactory.getLogger(InviteRebateController.class);

    @Autowired
    InviteRebateService inviteRebateService;
    @Autowired
    InviteRebateDao inviteRebateDao;

    /**
     * 获取返佣明细列表
     * @param userId(用户id)
     * @param limit(查询数量)
     * @return
     */
    @RequestMapping(value = "/getRebateDetail",method = {RequestMethod.GET})
    public ListResult GetRebateDetail(Long userId, Integer limit){
        List<RebateDetailOut> ssss = inviteRebateDao.getRebateDetail(userId, limit);
        ssss.forEach(s->{
            User user = beanDao.quickQueryOne(User.class, "id", s.getRefUserId());
            s.setHeadImage(user.getHeadimage());
            s.setName(user.getName());
        });
        return new ListResult<RebateDetailOut>()
                .setList(ssss)
                .setTotal(inviteRebateDao.getRebateDetailCount(userId));
    }

    /**
     * 保存邀请返佣充值明细
     * @param in
     * @return

    @RequestMapping(value = "/saveRebateDetail",method = {RequestMethod.POST})
    public GeneralResult<String> SaveRebateDetail(
            @RequestBody RebateDeatailIn in){
        double rate = Double.parseDouble(beanDao.quickQueryOne(SystemPara.class, "name", "inviteRebatePercentage").getValue());
        String ss =  inviteRebateService.SaveRebateDetail(in.getUserId(), in.getRefUserId(), in.getFee()*rate, in.getFee());
        return new GeneralResult<String>().setValue(ss);
    }
     */

    /**
     * 保存邀请人
     * @param in
     * @return
     */
    @RequestMapping(value = "/saveRef", method = {RequestMethod.POST})
    public GeneralResult saveRef(
            @RequestBody RebateDeatailIn in){
        if(in.getUserId().equals(in.getRefUserId())){
            throw new NoteException("本人不可以邀请本人！");
        }
        logger.info(String.format("in:%s----------------------------------------------%s",in.getUserId(),in.getRefUserId()));
        ActUser au = beanDao.quickQueryOne(ActUser.class,"userId", in.getUserId());
        if(au == null){
            throw new NoteException("怎么可能！");
        }
        if(au.getRefUserId() != null){
            return new GeneralResult<>();
        }
        au.setRefUserId(in.getRefUserId());
        beanDao.save(au);
        ReferenceWallet rw = beanDao.quickQueryOne(ReferenceWallet.class,"userId", in.getRefUserId());
        if(rw != null){
            return null;
        }
        rw = new ReferenceWallet();
        rw.setVersion(0);
        rw.setCreateDate(System.currentTimeMillis());
        rw.setUserId(in.getRefUserId());
        beanDao.save(rw);
        return new GeneralResult<>().setValue(rw);
    }

    /**
     * 提现
     * @param userId
     * @return
     */
    @RequestMapping(value = "/Transfer", method = {RequestMethod.GET})
    public GeneralResult<Boolean> Transfer(Long userId){
        Boolean flag = inviteRebateService.Transfer(userId);
        return new GeneralResult<Boolean>().setValue(flag);
    }

    /**
     * 获取钱包
     * @param userId
     * @return
     */
    @RequestMapping(value = "/GetWallet", method = {RequestMethod.GET})
    public GeneralResult GetWallet(Long userId){
        ReferenceWallet rw = inviteRebateDao.selectByUserId(userId);
        if(rw == null){
            Long now = System.currentTimeMillis();
            ReferenceWallet r = new ReferenceWallet();
            r.setUserId(userId);
            r.setCreateDate(10000000000000l+now);
            r.setVersion(0);
            beanDao.save(r);
            return new GeneralResult<ReferenceWallet>().setValue(r);
        }
        return new GeneralResult<ReferenceWallet>().setValue(rw);
    }
}
